import axios from '@/axios.config'

export default {
    state: {
        msg: '',
        depList: [],
        dutyList: [],
        dutyInfo: {},
        totleNum: 0,
        totleNum2: 0,
        verifyCode: 0
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg
        },
        add_depList(state, payload) {
            state.depList = [].concat(payload.com_deplist);
            state.totleNum = payload.com_total
        },
        add_dutyList(state, payload) {
            state.dutyList = [].concat(payload.com_dutylist);
            state.totleNum2 = payload.com_total
        },
        add_verify(state, payload) {
            state.verifyCode = payload.com_code
        },
        add_duty_info(state, payload) {
            state.dutyInfo = Object.assign({}, payload.com_dutyinfo)
        }
    },
    actions: {
        //创建部门
        createDepart({ commit }, payload) {
            return axios.post('/api/auth/org/save', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门列表分页
        getDepList({ commit }, payload) {
            return axios.post('/api/auth/org/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_depList',{
                            com_deplist: res.data.data.t,
                            com_total: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门删除
        delDep({ commit }, payload) {
            return axios.post('/api/auth/org/del',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "删除成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门开启
        openDep({ commit }, payload) {
            return axios.post('/api/auth/org/open',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "开启成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门关闭
        closeDep({ commit }, payload) {
            return axios.post('/api/auth/org/close',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "关闭成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门列表所有
        getDepAllList({ commit }, payload) {
            return axios.post('/api/auth/org/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_depList',{
                            com_deplist: res.data.data,
                        })
                    }else if(res.data.retCode == 9999995){
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //创建职责
        createDuty({ commit }, payload) {
            return axios.post('/api/auth/duty/save', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门列表分页
        getDutyList({ commit }, payload) {
            return axios.post('/api/auth/duty/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_dutyList',{
                            com_dutylist: res.data.data.t,
                            com_total: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //通过id获取单条职责数据
        getDutyById({ commit }, payload) {
            return axios.post('/api/auth/duty/info', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_duty_info',{
                            com_dutyinfo: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //职责删除
        delDuty({ commit }, payload) {
            return axios.post('/api/auth/duty/del',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "删除成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //职责开启
        openDuty({ commit }, payload) {
            return axios.post('/api/auth/duty/open',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "开启成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门关闭
        closeDuty({ commit }, payload) {
            return axios.post('/api/auth/duty/close',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "关闭成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //职责列表所有
        getAllDutyList({ commit }, payload) {
            return axios.post('/api/auth/duty/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_dutyList',{
                            com_dutylist: res.data.data,
                        })
                    }else if(res.data.retCode == 9999995){
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //职责立即调度
        dispatchDuty({ commit }, payload) {
            return axios.post('/api/auth/duty/refresh',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "调度成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
    }
}